package com.bezdelnick.forum.web.page;

import com.bezdelnick.forum.domain.Answer;
import com.bezdelnick.forum.domain.Question;
import com.bezdelnick.forum.service.ForumService;
import com.bezdelnick.forum.web.component.AnswersListView;
import com.bezdelnick.forum.web.page.template.UserMainMenuTemplate;
import com.bezdelnick.forum.web.panel.ModeratorMainMenuPanel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.util.HtmlUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 23.07.12
 * Time: 16:49
 * To change this template use File | Settings | File Templates.
 */
public class ShowQuestionPage extends UserMainMenuTemplate {
    private static final Logger LOGGER = LoggerFactory.getLogger(ShowQuestionPage.class);

    @SpringBean
    @SuppressWarnings("unused")
    private ForumService forumService;

    private Question question;
    private List<Answer> answers;

    public ShowQuestionPage(PageParameters parameters) {
        if (!parameters.get("id").isNull()) {
            long deliveredId = parameters.get("id").toLong();
            try {
                this.question = forumService.getQuestionDao().getQuestionById(deliveredId);
                this.answers = forumService.getAnswerDao().loadAnswersByQuestionId(deliveredId);
            } catch (Exception e) {
                this.question = new Question();
                this.answers = new ArrayList<Answer>();
                LOGGER.error(e.getMessage());
            }
        }

        // add body component
        Label lblTitleQuestion = new Label("title", question.getTitle());
        add(lblTitleQuestion);

        Label lblTypeQuestion = new Label("type", forumService.getTypeDao().getTypeById(question.getTypeId()).getName());
        add(lblTypeQuestion);

        Label lblSubtypeQuestion = new Label("subtype", forumService.getSubtypeDao().getSubtypeById(question.getSubtypeId()).getName());
        add(lblSubtypeQuestion);

        Label lblTextQuestion = new Label("text", HtmlUtils.htmlUnescape(question.getTextQuestion()));
        lblTextQuestion.setEscapeModelStrings(false);
        add(lblTextQuestion);

        BookmarkablePageLink bplAddAnswer = new BookmarkablePageLink("goto_add_answer", AddAnswerPage.class);
        bplAddAnswer.getPageParameters().add("id", question.getId());
        add(bplAddAnswer);

        AnswersListView answersListView = new AnswersListView("list_answers", this.answers);
        add(answersListView);
    }
}
